import { IStageAssetsInfo, Stage } from 'babylon-lib/base/stage';
import { AbstractAssetTaskInfo, ArcRotateCamera, Camera, GLTFMeshTaskInfo, ILoose, Light, Scene, TextureTaskInfo } from 'babylon-lib/index';
import { HUB_SCOPE, MessageHub } from '../global/message-hub';
import { IMessageSetup, MESSAGE_HUB_TYPE } from '../global/global-constant';
import { RawAssetsUrl } from '../raw-asset-info';
import { NavigateInput } from './controller/navigate-input';
import { DeviceInputPreset } from 'babylon-lib/preset/device-input/device-input-preset';
import { CommunityManager } from './community/community-manager';
import { LandscapeManager } from './landscape/landscape-manager';
import { BlocManager } from './bloc/bloc-manager';
import { SelectedMark } from './attachment/selected-mark';
import { SelectManager } from './select/select-manager';
import { Random } from 'babylon-lib/tool/random';


class DefaultStage extends Stage {

    protected assetTaskInfos(): ILoose<AbstractAssetTaskInfo> | Array<IStageAssetsInfo> {
        return Object.assign({}, [
            new GLTFMeshTaskInfo(RawAssetsUrl.modelDixing),
            new GLTFMeshTaskInfo(RawAssetsUrl.modelCheng),
            new GLTFMeshTaskInfo(RawAssetsUrl.modelZhen),
            new GLTFMeshTaskInfo(RawAssetsUrl.modelGuan),
            new GLTFMeshTaskInfo(RawAssetsUrl.modelCun),
            new GLTFMeshTaskInfo(RawAssetsUrl.modelChengweizhi),
            new GLTFMeshTaskInfo(RawAssetsUrl.modelZhenweizhi),
            new GLTFMeshTaskInfo(RawAssetsUrl.modelGuanweizhi),
            new GLTFMeshTaskInfo(RawAssetsUrl.modelCunweizhi),

            new TextureTaskInfo(RawAssetsUrl.textureCircle),
            new TextureTaskInfo(RawAssetsUrl.textureTouchCenter),
            new TextureTaskInfo(RawAssetsUrl.textureTouchOuter),
            new TextureTaskInfo(RawAssetsUrl.textureWater),
            new TextureTaskInfo(RawAssetsUrl.textureSmoke),
        ]) as unknown as ILoose<AbstractAssetTaskInfo>;
    }

    protected initializeLights(scenes: Array<Scene>, cameras: Array<Camera>): Array<Light> {
        return [];
    }

    protected initializeCameras(scenes: Array<Scene>): Array<Camera> {
        const cameras = super.initializeCameras(scenes);
        const cam = cameras[0] as ArcRotateCamera;
        cam.minZ = 0.1;
        cam.maxZ = 1000;
        cam.lowerRadiusLimit = 5;
        cam.upperRadiusLimit = 50;
        cam.lowerBetaLimit = 0.1;
        cam.upperBetaLimit = Math.PI * 0.4;
        return cameras;
    }

    protected async initializeComponents(scenes: Array<Scene>, cameras: Array<Camera>, lights: Array<Light>): Promise<void> {
        /**
         * 初始化游戏组件
         */
        Random.useFixed = true;

        SelectManager.initialize(this);
        LandscapeManager.initialize(this);
        DeviceInputPreset.initialize(this.mainScene);
        new NavigateInput(this);
        SelectedMark.setup(this);

        const hub = new MessageHub<MESSAGE_HUB_TYPE>(window.parent, HUB_SCOPE.GAME);
        this.onReady = () => {
            hub.dispatch(MESSAGE_HUB_TYPE.IFRAME_READY, '');
        };
        hub.listen<IMessageSetup>(MESSAGE_HUB_TYPE.SETUP, (e) => {
            Random.index = e.data.seed;
            CommunityManager.initialize(this);
            BlocManager.initialize(this, e.data);
            /**
             * 发送就绪消息给父页面，父页面发送到服务器？
             * or
             * 此处直接发送到服务器？
             */
            hub.dispatch(MESSAGE_HUB_TYPE.GAME_READY,{
                player:e.data.user
            });
        });
    }

}


export { DefaultStage };